//
//  LLClusterPopView.m
//  LoveLove
//
//  Created by 肖兰月 on 2019/4/8.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLClusterPopView.h"
#import "LLMallDoModel.h"

@interface LLClusterPopView ()
@property (strong, nonatomic) LLButton *bgCover;
@property (strong, nonatomic) UIView *contentView;
@property (strong, nonatomic) UILabel *titleLbl;
@property (strong, nonatomic) LLButton *closeBtn;
@property (strong, nonatomic) UILabel *noteLbl;
@property (strong, nonatomic) LLButton *clusterBtn;

@property (strong, nonatomic) NSMutableArray *totalImgViewArray;
@property (strong, nonatomic) NSMutableArray *showImgViewArray;

@property (strong, nonatomic) UILabel *masterLbl;
@end

@implementation LLClusterPopView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews {
    _bgCover = [LLButton new];
    _bgCover.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    _bgCover.frame = self.bounds;
    [self addSubview:_bgCover];
    
    CGFloat left = LLScreenWidth < 375 ? 30 : 40;
    CGFloat contentW = self.width-left*2;
    _contentView = UIView.new;
    _contentView.frame = CGRectMake(left, (self.height-200)*0.3, contentW, 230);
    _contentView.backgroundColor = UIColor.whiteColor;
    _contentView.layer.cornerRadius = 7;
    _contentView.clipsToBounds = YES;
    [self addSubview:_contentView];
    
    _closeBtn = [LLInitBaseViewTool buttonWithTitle:nil image:@"close" titleColor:nil font:0];
    _closeBtn.frame = CGRectMake(self.contentView.right-15, self.contentView.top-15, 30, 30);
    _closeBtn.backgroundColor = LLColorRGB(160);
    _closeBtn.layer.cornerRadius = 15;
    _closeBtn.size = CGSizeMake(30, 30);
    [_closeBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_closeBtn];
    
    _titleLbl = [LLInitBaseViewTool labelWithText:nil textColor:LLColorRGB(0) font:18 textAlignment:NSTextAlignmentCenter];
    _titleLbl.frame = CGRectMake(0, 0, _contentView.width, 60);
    [_contentView addSubview:_titleLbl];
    
    _noteLbl = [LLInitBaseViewTool labelWithText:nil textColor:nil font:14 textAlignment:NSTextAlignmentCenter];
    _noteLbl.frame = CGRectMake(0, _titleLbl.bottom, _contentView.width, 14);
    [_contentView addSubview:_noteLbl];
    
    _clusterBtn = [LLInitBaseViewTool buttonWithTitle:nil image:nil titleColor:UIColor.whiteColor font:17];
    _clusterBtn.frame = CGRectMake(30, _contentView.height-44-20, _contentView.width-60, 44);
    _clusterBtn.layer.cornerRadius = 22;
    _clusterBtn.backgroundColor = LLColor(225, 0, 0);
    [_clusterBtn addTarget:self action:@selector(clusterBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:_clusterBtn];
}

- (void)setModel:(LLMallDoModel *)model {
    _model = model;
    _titleLbl.text = [NSString stringWithFormat:@"参与%@的拼团",model.nickname];
    NSString *noteStr = [NSString stringWithFormat:@"仅剩%@个名额，%@", model.countStr, model.remainingTime];
    _noteLbl.attributedText = [NSAttributedString attributedStringsWithText:noteStr textFont:14 textRange:NSMakeRange(2, model.countStr.length) textColor:LLColor(225, 0, 0) isBoldFont:NO];
}

- (void)setClusterTitle:(NSString *)clusterTitle {
    _clusterTitle = clusterTitle;
    [_clusterBtn setTitle:clusterTitle forState:0];
}

- (void)setImgs:(NSArray *)imgs {
    _imgs = imgs;
    [self.showImgViewArray removeAllObjects];
    for (NSInteger i = 0; i < imgs.count; i++) {
        NSInteger totalImgViewCount = self.totalImgViewArray.count;
        UIImageView *imgView;
        if (i <= totalImgViewCount-1) {
            imgView = self.totalImgViewArray[i];
        } else {
            imgView = UIImageView.new;
            imgView.layer.cornerRadius = 25;
            imgView.clipsToBounds = YES;
            [self.contentView addSubview:imgView];
            [self.totalImgViewArray addObject:imgView];
        }
        if (i == 0) {
            imgView.layer.borderColor = LLColor(225, 0, 0).CGColor;
            imgView.layer.borderWidth = 1;
            [imgView addSubview:self.masterLbl];
        }
        [imgView sd_setImageWithURL:LLImageWithUrl(imgs[i]) placeholderImage:[UIImage imageNamed:@"clusterPlaceHolder"] options:SDWebImageRefreshCached];
        [self.showImgViewArray addObject:imgView];
    }
}

- (void)clusterBtnClick {
    if ([self.delegate respondsToSelector:@selector(clusterPopViewDidClickCluster)]) {
        [self.delegate clusterPopViewDidClickCluster];
    }
}

- (void)show {
    self.hidden = NO;
}

- (void)hide {
    self.hidden = YES;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat margin = 20;
    CGFloat top = self.noteLbl.bottom+20;
    CGFloat imgViewW = 50;
    CGFloat left = (self.contentView.width-margin-imgViewW*2)/2.0;
    for (NSInteger i = 0; i < self.showImgViewArray.count; i++) {
        UIImageView *imgView = self.showImgViewArray[i];
        imgView.frame = CGRectMake(left+(imgViewW+margin)*i, top, imgViewW, imgViewW);
    }
}

#pragma mark - lazy load

- (NSMutableArray *)totalImgViewArray {
    if (!_totalImgViewArray) {
        _totalImgViewArray = NSMutableArray.new;
    }
    return _totalImgViewArray;
}

- (NSMutableArray *)showImgViewArray {
    if (!_showImgViewArray) {
        _showImgViewArray = NSMutableArray.new;
    }
    return _showImgViewArray;
}

- (UILabel *)masterLbl {
    if (!_masterLbl) {
        _masterLbl = [LLInitBaseViewTool labelWithText:@"拼主" textColor:UIColor.whiteColor font:10 textAlignment:NSTextAlignmentCenter];
        _masterLbl.frame = CGRectMake(0, -6, 28, 16);
        _masterLbl.layer.cornerRadius = 8;
    }
    return _masterLbl;
}

@end
